﻿using System.Diagnostics;
using LFSRecord2.Misc;

namespace LFSRecord2.Model.UserActions
{
    public class UserActionKeyFrameStringValueChanged : UserActionBase
    {
        // Action description, so we can replay this action (and reverse it)
        public int LayerIndex { get; set; }
        public int PropertyIndex { get; set; }
        public int KeyFrameIndex { get; set; }
        public string OldValue { get; set; }
        public string NewValue { get; set; }

        public UserActionKeyFrameStringValueChanged()
        {
            Action = UserActionTypes.KeyFrameStringValueChanged;
        }

        public override void DoUndo()
        {
            Debug.WriteLine("Undoing a KeyFrameStringValueChanged action");
            if (KeyFrameIndex == -1)
            {
                ((CompLayerPropertyString)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentValue = OldValue;
            }
            else
            {
                if (KeyFrameIndex > LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames.Count - 1)
                {
                    Logger.LogError("Invalid keyframe reference while undoing kfStringValueAction");
                }
                CustomStringKeyFrame kfs = (CustomStringKeyFrame)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames[KeyFrameIndex];
                kfs.Value = OldValue;
            }
            LfsRecordController.project.Layers[LayerIndex].UpdatedCurrentValues = CompLayerPropertyUpdateTypes.All;
        }

        public override void DoRedo()
        {
            Debug.WriteLine("Redoing a KeyFrameStringValueChanged action");
            if (KeyFrameIndex == -1)
            {
                ((CompLayerPropertyString)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentValue = NewValue;
            }
            else
            {
                if (KeyFrameIndex > LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames.Count - 1)
                {
                    Logger.LogError("Invalid keyframe reference while redoing kfStringValueAction");
                }
                ((CustomStringKeyFrame)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames[KeyFrameIndex]).Value = NewValue;
            }
            LfsRecordController.project.Layers[LayerIndex].UpdatedCurrentValues = CompLayerPropertyUpdateTypes.All;
        }
    }
}
